package com.example.leetdemo.plan1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Sword033 {
    public List<List<String>> groupAnagrams(String[] strs) {
        int[] t = new int[] {
                2,3,5,7,11,13,17,
                19,23,29,31,37,41,43,
                47,53,59,61,67,71,
                73,79,83,89,97,101,103
        };
        Map<Long, List<String>> map = new HashMap<>();
        for (String str : strs) {
            long l = 1L;
            for (int i = 0; i < str.length(); i++) {
                l *= t[str.charAt(i) - 'a'];
            }
            if (l != 1L) {
                List<String> list = map.containsKey(l) ? map.get(l) : new ArrayList<>();
                list.add(str);
                map.put(l, list);
            }
        }
        List<List<String>> list = new ArrayList<>();
        map.forEach((k,v) -> list.add(v));
        return list;
    }
}
